/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.MessageNamespaces;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.service.CommentService;
import etcore.webapp.grouptalk.common.service.MessageService;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.UserService;

/**
 * This action validates the request and executes the command of getting my
 * messages,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class UserCommentsAction extends AbstractAction {

	private static final long serialVersionUID = 4599787097897244199L;
	// validate result
	public static final int ACCESS_ALLOWED = 0;
	public static final int ERROR = 1;
	public static final int REMOVE_ALLOWED = 2;
	// utility constants

	public static final String START_PID = "startpid";
	public static final String END_PID = "endpid";
	public static final String COMMENT_NUM = "commentNum";
	public static final String LAST_COMMENT = "lastComment";
	public static final String REMOVE_COMMENT = "removeComment";

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		String userName = request.getParameter(PageConstants.TARGET_USERNAME);
		if (userName != null) {
			try {
				userName = new String(userName.getBytes(PageConstants.ENCODING_ISO88591),
						(PageConstants.ENCODING_UTF8));
			} catch (Exception e) {
				e.printStackTrace();
			}
			UserService userService = new UserService();
			User result = userService.findByUserName(userName);
			// System.out.println(result.getUserName());
			if (result != null) {
				request.setAttribute(PageConstants.TARGET_USER, result);
				return ACCESS_ALLOWED;
			}
		}
		return ERROR;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (ERROR == validationResult)
			return PageConstants.ACTION_PATH_ERROR;
 
		return PageConstants.PAGE_PATH_USERCOMMENTS;
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
	 
		if (ACCESS_ALLOWED == validationResult
				 ) {
			getComments(request, response);
		}

	}

	 

	@SuppressWarnings("rawtypes")
	private void getComments(HttpServletRequest request,
			HttpServletResponse response) {
		int intpageid = 1;
 
		User targetUser = (User) request
		.getAttribute(PageConstants.TARGET_USER);
 
		CommentService commentService = new CommentService();

		int unit = 10;

		List comments = null;
		long all = 0;
		int begin = 0, pagenums = 0;
		String pageid = request.getParameter(PageConstants.PAGEID);

		if (pageid != null)
			intpageid = Integer.parseInt(pageid);

		all = commentService.findPublicCommentsSizeByProperty(PageConstants.USERNAME,
				targetUser.getUserName());

		pagenums = (int) ((all + unit - 1) / unit);
		begin = unit * (intpageid - 1);
		MessageService messageService = new MessageService();
		comments = commentService.findPublicCommentsByProperty(PageConstants.USERNAME,
				targetUser.getUserName(), begin, unit);
		if (comments == null)
			return;
		for (Iterator iter=comments.iterator();iter.hasNext(); ) {
			Object[]comment=(Object[])iter.next();
	 
			
			List msgs=messageService.findByProperty(PageConstants.ID, comment[1], 0, 1);
			if(msgs!=null&&msgs.size()>0){
			Object[] msg=(Object[])msgs.get(0);
			request.setAttribute(comment[0]+PageConstants.MSG, msg);
			}
		}
	
		int startpid = intpageid - 3;
		if (startpid <= 0)
			startpid = 1;
		int endpid = intpageid + 8;
		if (intpageid > 8)
			endpid = intpageid + 6;
		if (endpid >= pagenums)
			endpid = pagenums;

		request.setAttribute(START_PID, startpid);
		request.setAttribute(END_PID, endpid);

		request.setAttribute(PageConstants.COMMENTS, comments);
		request.setAttribute(PageConstants.PAGENUMS, pagenums);
		request.setAttribute(PageConstants.ALL, all);
		request.setAttribute("userCommentsCount", all);
		request.setAttribute(PageConstants.UNIT, unit);
		request.setAttribute(PageConstants.INTPAGEID, intpageid);

	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#
	 * buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> namespaces = new ArrayList<String>();
		namespaces.add(MessageNamespaces.USER_PROFILE);
		return namespaces;
	}

}
